package main

import (
	"fmt"
)

type Obj struct {
	Name string
}

func copySlice() {
	s1 := make([]int, 0, 20)
	s2 := []int{1, 2, 3, 4, 5}
	// copy 只能把原切片中的数据复制到目标切片中已经初始化了的位置
	copy(s1[5:5+len(s2)], s2)
	fmt.Println(s1[0:20])
}
func main() {
	copySlice()
	s1 := make([]int, 0, 10)
	s1 = append(s1, 1, 2, 3, 4, 5, 6)
	s2 := append(s1, 7)
	s3 := s1

	fmt.Println(&s1)
	fmt.Println(&s2)
	fmt.Println(&s3)
	fmt.Printf("s1:%p\r\ns2:%p\ns3:%p\n", s1, s2, s3)
	fmt.Println(&s1 == &s3)
	fmt.Println(&s1 == &s2)

	aint := 2
	bint := aint
	fmt.Printf("aint:%p\r\nbint:%p\n", &aint, &bint)

	aO := Obj{}
	bO := &aO
	aO.Name = "sss"
	fmt.Printf("aO:%p\r\nbO:%p\n", &aO, bO)
	fmt.Printf("aO:%s\r\nbO:%s\n", aO.Name, bO.Name)

	testPageUtil()

}

// 分页处理slice工具---begin----

func testPageUtil() {
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
	page := newPageUtil(len(arr), 3)
	for page.start(); page.OK; page.next() {
		fmt.Println(arr[page.Begin:page.End])
	}
}

type pageUtil struct {
	len   int
	index int
	limit int
	OK    bool
	Begin int
	End   int
}

func newPageUtil(len int, limit int) *pageUtil {
	return &pageUtil{
		len:   len,
		index: 0,
		limit: limit,
		OK:    true,
		Begin: 0,
		End:   0,
	}
}
func (p *pageUtil) start() {
	p.next()
}

func (p *pageUtil) next() {
	if p.index < p.len {
		p.Begin = p.index
		if p.End = p.index + p.limit; p.End >= p.len {
			p.End = p.len
		}
		p.index = p.End
		p.OK = true
		return
	} else {
		p.OK = false
		return
	}
}

// 分页处理slice工具---end----
